/* @(#)GSMarketInfoFileDownloadAction.java
 * 
 *  Project:GS
 *  
 *  Modify  Information:
 *  ===============================================
 *   Author          Date            Description
 *  -------------- ---------------  ---------------
 *  Jenny  Li      2006-5-26          新增
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 *
 */
package com.hitrust.gs.action;

import java.io.File;
import java.io.FileInputStream;

import com.hitrust.gs.business.GSMarketInfoProcessor;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.lib.GSSystemConfigurator;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.service.GSBaseManager;
import com.hitrust.gs.util.PubTool;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.vo.GSMarketInfoQryVO;

/**
 * @author JennyLi
 *
 */
public class GSMarketInfoFileDownloadAction extends GSBaseAction {

    /* (non-Javadoc)
     * @see com.hitrust.gs.service.GSBaseAction#execute(com.hitrust.gs.service.GSBaseForm)
     */
    public String execute(GSBaseForm aForm) throws Exception {
        String tCreateDate = aForm.getRequest().getParameter("createDate");
        GSMarketInfoProcessor tMarketProc = new GSMarketInfoProcessor();
        GSMarketInfoQryVO tVO = tMarketProc.getMarketInfoByCD(tCreateDate);
        String tName = tVO.fileName;
        GSLogWriter.info("fileName=" + tName);
        
        try{
            //取得保存路径
            String tPath = GSSystemConfigurator.getParam("gs.upload.market.savepath");
            String tFileName = PubTool.apendSepa(tPath) + tName;
            File tFile = new File(tFileName);
            if(!tFile.isFile()){
                GSLogWriter.error(this,"文件不存在");
                throw new GSException("文件不存在");
            }
            FileInputStream tFileStream = new FileInputStream(tFile);
            long length = tFile.length();
            byte abyte[] = new byte[1024];
            aForm.getResponse().setContentType("application/x-msdownload");
            aForm.getResponse().setContentLength((int)length);
            aForm.getResponse().setHeader("Content-Disposition", "attachment; filename=" 
                            + GSBaseManager.toUtf8String(tName));
            
            int k = 0;
            while((long)k < length)
            {
                int j = tFileStream.read(abyte, 0, 1024);
                k += j;
                aForm.getResponse().getOutputStream().write(abyte, 0, j);
            }
            tFileStream.close();
            aForm.getResponse().getOutputStream().flush();
            aForm.getResponse().getOutputStream().close();
            GSLogWriter.debug("文件下载成功");
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw e;
        }
        return GSBaseConstants.FORWARD_SUCCESS;
    }

}
